class QuestionsController < ApplicationController
  def create
    user = User.find_by_id(session[:user_id])
    category = Category.find_by_id(params[:category])
    @questions = Question.new
    question_voter  = QuestionVoter.new
    @questions.message = params[:message]
    question_voter.member_voter = 0
    question_voter.remote_voter = '0'
    @questions.agree = 0
    @questions.disagree = 0
    @questions.category = (category)
    user.questions << (@questions)
    if @questions.valid?
      if @questions.save
        @questions.question_voters << question_voter
        flash[:question_ss] = "Your question has been added"
      end
    else
      flash[:question_err] = "The question is not valid"
    end
    redirect_to :controller =>"workspaces", :action =>"question"
  end

  def index
    @category = Category.all
    if !params[:cat].nil?
      @question = Question.paginate(:all, :per_page => 1,:page => params[:page],:conditions =>["category_id = ?",params[:cat]])
    else
      @question = Question.paginate(:all, :per_page => 1,:page => params[:page])
    end
  end
end